/*
 * 作者：坚果派
 * 公众号：nutpi
 * 电话：17752170152
 * 官网：https://www.nutpi.net/
 */

import 'package:flutter/foundation.dart';
import '../models/address_model.dart';
import '../services/address_service.dart';

class AddressProvider with ChangeNotifier {
  final AddressService _addressService = AddressService();
  
  // 全国城市列表
  List<Province> _provinces = [];
  List<Province> get provinces => _provinces;
  bool _loadingProvinces = false;
  bool get loadingProvinces => _loadingProvinces;
  String _provinceError = '';
  String get provinceError => _provinceError;

  // 世界国家城市列表
  List<Country> _countries = [];
  List<Country> get countries => _countries;
  bool _loadingCountries = false;
  bool get loadingCountries => _loadingCountries;
  String _countryError = '';
  String get countryError => _countryError;

  // 搜索结果
  List<Province> _searchResults = [];
  List<Province> get searchResults => _searchResults;
  bool _loadingSearch = false;
  bool get loadingSearch => _loadingSearch;
  String _searchError = '';
  String get searchError => _searchError;

  // 简化版省份列表
  List<ProvinceSimple> _provinceSimples = [];
  List<ProvinceSimple> get provinceSimples => _provinceSimples;
  bool _loadingProvinceSimples = false;
  bool get loadingProvinceSimples => _loadingProvinceSimples;
  String _provinceSimpleError = '';
  String get provinceSimpleError => _provinceSimpleError;

  // 城市列表
  List<CitySimple> _cities = [];
  List<CitySimple> get cities => _cities;
  bool _loadingCities = false;
  bool get loadingCities => _loadingCities;
  String _cityError = '';
  String get cityError => _cityError;

  // 区列表
  List<AreaSimple> _areas = [];
  List<AreaSimple> get areas => _areas;
  bool _loadingAreas = false;
  bool get loadingAreas => _loadingAreas;
  String _areaError = '';
  String get areaError => _areaError;

  // 街道列表
  List<StreetSimple> _streets = [];
  List<StreetSimple> get streets => _streets;
  bool _loadingStreets = false;
  bool get loadingStreets => _loadingStreets;
  String _streetError = '';
  String get streetError => _streetError;

  // 村委会列表
  List<VillageSimple> _villages = [];
  List<VillageSimple> get villages => _villages;
  bool _loadingVillages = false;
  bool get loadingVillages => _loadingVillages;
  String _villageError = '';
  String get villageError => _villageError;

  // 获取全国城市列表
  Future<void> fetchProvinces() async {
    _loadingProvinces = true;
    _provinceError = '';
    notifyListeners();

    try {
      _provinces = await _addressService.getProvinceList();
      _loadingProvinces = false;
      notifyListeners();
    } catch (e) {
      _loadingProvinces = false;
      _provinceError = e.toString();
      notifyListeners();
    }
  }

  // 搜索城市
  Future<void> searchAddress(int type, String value) async {
    if (value.isEmpty) {
      _searchResults = [];
      notifyListeners();
      return;
    }

    _loadingSearch = true;
    _searchError = '';
    notifyListeners();

    try {
      _searchResults = await _addressService.searchAddress(type, value);
      _loadingSearch = false;
      notifyListeners();
    } catch (e) {
      _loadingSearch = false;
      _searchError = e.toString();
      notifyListeners();
    }
  }

  // 获取世界国家城市列表
  Future<void> fetchCountries() async {
    _loadingCountries = true;
    _countryError = '';
    notifyListeners();

    try {
      _countries = await _addressService.getWorldCountryList();
      _loadingCountries = false;
      notifyListeners();
    } catch (e) {
      _loadingCountries = false;
      _countryError = e.toString();
      notifyListeners();
    }
  }

  // 获取省份简单列表
  Future<void> fetchProvinceSimples() async {
    _loadingProvinceSimples = true;
    _provinceSimpleError = '';
    notifyListeners();

    try {
      _provinceSimples = await _addressService.getProvinceSimpleList();
      _loadingProvinceSimples = false;
      notifyListeners();
    } catch (e) {
      _loadingProvinceSimples = false;
      _provinceSimpleError = e.toString();
      notifyListeners();
    }
  }

  // 获取城市列表
  Future<void> fetchCities(String provinceCode) async {
    _loadingCities = true;
    _cityError = '';
    notifyListeners();

    try {
      _cities = await _addressService.getCityList(provinceCode);
      _loadingCities = false;
      notifyListeners();
    } catch (e) {
      _loadingCities = false;
      _cityError = e.toString();
      notifyListeners();
    }
  }

  // 获取区列表
  Future<void> fetchAreas(String provinceCode, String cityCode) async {
    _loadingAreas = true;
    _areaError = '';
    notifyListeners();

    try {
      _areas = await _addressService.getAreaList(provinceCode, cityCode);
      _loadingAreas = false;
      notifyListeners();
    } catch (e) {
      _loadingAreas = false;
      _areaError = e.toString();
      notifyListeners();
    }
  }

  // 获取街道列表
  Future<void> fetchStreets(String provinceCode, String cityCode, String areaCode) async {
    _loadingStreets = true;
    _streetError = '';
    notifyListeners();

    try {
      _streets = await _addressService.getStreetList(provinceCode, cityCode, areaCode);
      _loadingStreets = false;
      notifyListeners();
    } catch (e) {
      _loadingStreets = false;
      _streetError = e.toString();
      notifyListeners();
    }
  }

  // 获取村委会列表
  Future<void> fetchVillages(String provinceCode, String cityCode, String areaCode, String streetCode) async {
    _loadingVillages = true;
    _villageError = '';
    notifyListeners();

    try {
      _villages = await _addressService.getVillageList(provinceCode, cityCode, areaCode, streetCode);
      _loadingVillages = false;
      notifyListeners();
    } catch (e) {
      _loadingVillages = false;
      _villageError = e.toString();
      notifyListeners();
    }
  }

  // 清除搜索结果
  void clearSearchResults() {
    _searchResults = [];
    notifyListeners();
  }

  // 清除城市列表
  void clearCities() {
    _cities = [];
    notifyListeners();
  }

  // 清除区列表
  void clearAreas() {
    _areas = [];
    notifyListeners();
  }

  // 清除街道列表
  void clearStreets() {
    _streets = [];
    notifyListeners();
  }

  // 清除村委会列表
  void clearVillages() {
    _villages = [];
    notifyListeners();
  }
}